package com.mask.common.mq.handler;

import com.mask.common.mq.model.MessageDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

@Slf4j
public abstract class MessageHandler<T> {
    /**
     * 处理消息
     */
    @RabbitListener(queues = "${mask.mq.queues.${queueName}.name}")
    public void handleMessage(MessageDTO messageDTO) {
        try {
            log.info("收到消息：messageId={}, messageType={}", 
                messageDTO.getMessageId(), messageDTO.getMessageType());
            
            @SuppressWarnings("unchecked")
            T content = (T) messageDTO.getContent();
            process(content, messageDTO);
            
        } catch (Exception e) {
            log.error("处理消息失败：messageId={}", messageDTO.getMessageId(), e);
            throw e;
        }
    }

    /**
     * 处理具体的业务逻辑
     */
    protected abstract void process(T content, MessageDTO messageDTO);
}